Skip to main content

3.2 Variable recoding - recode

The recode command can be used to recode variables, as an alternative to replace. This is a useful tool when recoding many values/categories within a single variable. The command makes it possible to complete the full set of recodings in a single command expression, which contributes to shorter processing time. It is also possible to recode multiple variables at a time, using recode.

Example on coding the variable "male" derived from BEFOLKNING_KJOENN (contains data on gender where male = '1' and female = '2') using recode:

 
import ds/BEFOLKNING_KJOENN as male
destring male
recode male (2 = 0)
 

It is possible to use recode on both numeric and alphanumerical variables, and you can also create value labels for the recoded values inside the recode expression itself.

Examples on how to encode groups of numbers for the variables var1 and var2:

  • recode var1 var2 (1 2 3 = 0) (values 1-3 recoded into 0)

  • recode var1 var2 (1/7 = 0) (values 1-7 recoded into 0)

  • recode var1 var2 (1/7 = 0) (nonmissing = 1) (missing = 99) (other valid values recoded into 1, missing values recoded into 99)

  • recode var1 var2 (1/7 = 0) (* = 99) (all other values recoded into 99)

  • recode var1 var2 (min/100 = 1)(101/max = 2) (all values 100 or lower recoded into 1, all values 101 or greater recoded into 2)

  • recode regstat ('3' '5' = '0' 'not-resident') (the values '3' and '5' for the alphanumerical variable regstat are recoded into '0' and are given the value lable "not-resident")


IMPORTANT INFO
  • The parameters min and max can only be used in connection with interval indications, as in the example above.

  • Alphanumerical values ​​cannot be included in interval entries, only as individual values, cf. the example above.

  • Alphanumerical values ​​and value labels can be specified with both single and double quotation marks


For more information about recode, use the help recode command. This also shows an overview of associated options.